package com.gxa.ehome.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.ehome.entity.Admin;
import com.gxa.ehome.entity.Cp;
import com.gxa.ehome.exception.BusinessException;
import com.gxa.ehome.service.AdminService;
import com.gxa.ehome.service.CpService;
import com.gxa.ehome.service.impl.AdminServiceImpl;
import com.gxa.ehome.service.impl.CpServiceImpl;
import com.gxa.ehome.util.JsonResult;
import com.gxa.ehome.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@WebServlet(name = "CpnListServlet", urlPatterns = "/cp")
public class CpnListServlet extends HttpServlet {
    private CpService cpService = new CpServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理请求中的中文, 使用UTF-8编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");   // 服务器端的字符集设置(影响流)- java代码相关
        response.setHeader("Content-Type", "text/html;charset=UTF-8"); // 浏览器行为

        JsonResult jsonResult = new JsonResult();
        String method = request.getHeader("method");
//        System.out.println(method);
        switch (method) {
            case "selectByPage": {
                String page = request.getParameter("page");
                String limit = request.getParameter("limit");
//                System.out.println(page);
//                System.out.println(limit);
                try {
                    List<Cp> locps = cpService.selectAllByPage(Integer.parseInt(page), Integer.parseInt(limit));
                    jsonResult.setCode("0");
                    jsonResult.setData(locps);
                    jsonResult.setValue("count",cpService.selectAllCp().size());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "addData": {
                String formData = request.getParameter("formData"); //前端所传的所有字段
                Cp cp = JSON.parseObject(formData, Cp.class);
                System.out.println(formData);
                System.out.println(cp);
                try {
                    Integer lines = cpService.insert(cp);
                    if (lines > 0) {
                        jsonResult.setCode("0");
                        jsonResult.setMsg("添加成功");
                    } else {
                        jsonResult.setCode("1");
                        jsonResult.setMsg("添加失败");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "upData": {
                String formData = request.getParameter("formData"); //前端所传的所有字段
                Cp cp = JSON.parseObject(formData, Cp.class);
                System.out.println(formData);
                System.out.println(cp);
                try {
                    Integer lines = cpService.update1(cp.getId(), cp);
                    if (lines > 0) {
                        jsonResult.setCode("0");
                        jsonResult.setMsg("修改成功");
                    } else {
                        jsonResult.setCode("1");
                        jsonResult.setMsg("修改失败,清重试");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            default: {
                break;
            }
        }
        ResponseUtil.send(response, JSON.toJSONString(jsonResult.getValues()));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
